package com.example.myservice_provider.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.myservice_provider.entity.Comment;
import com.example.myservice_provider.entity.Post;
import com.example.myservice_provider.entity.Response;
import com.example.myservice_provider.entity.Users;
import com.example.myservice_provider.service.PostService;
import com.example.myservice_provider.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/posts")
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private UserService userService;

    @PostMapping(value = {"/getAllPosts"})
    public Response getAllPosts(Integer page, String classification, Integer posterId) throws  Exception{
        Response response =new Response();

        Page<Post> allPosts = this.postService.getAllPosts(page, classification, posterId);

        response.setHead("OK");
        response.setCode(1);
        response.setData(allPosts);
        return response;
    }


    @PostMapping(value = {"/getPostDetail"})
    public Response getPostDetail(Integer postID) throws  Exception{
        Response response =new Response();

        Post onePost = this.postService.getOnePost(postID);
        System.out.println(onePost);
        if(onePost!=null){
            response.setCode(1);
        }else{
            response.setCode(0);
        }
        response.setHead("OK");
        response.setData(onePost);

        return response;
    }
    @PostMapping(value = {"/starPost"})
    public Response starPost(Integer postID) throws  Exception{
        Response response =new Response();
        response.setHead("OK");
        response.setCode(this.postService.starPost(postID));
        return response;
    }

    @PostMapping(value = {"/updatePost"})
    public Response updatePost(@RequestBody Post post) throws  Exception{
        Response response =new Response();

        response.setHead("OK");
        response.setCode(this.postService.updatePost(post));
        return response;
    }

    @PostMapping(value = {"/addPost"})
    public Response addPost(@RequestBody Post post) throws  Exception{
        Response response =new Response();

        response.setHead("OK");
        response.setCode(this.postService.addPost(post));
        return response;
    }

//    @PostMapping(value = {"/getAllPosts"})
//    public Response getAllPosts( Integer page, String classification,Integer posterId) throws  Exception{
//        Response response =new Response();
//
//        Page<Post> allPosts = this.postService.getAllPosts(page, classification, posterId);
//
//        response.setHead("OK");
//        response.setCode(1);
//        response.setData(allPosts);
//
//        return response;
//    }
}
